package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.MsgResult;
import com.itheima.common.PageResult;
import com.itheima.common.R;
import com.itheima.dao.TbActivityDao;
import com.itheima.dto.ActivityDto;
import com.itheima.pojo.TbActivity;
import com.itheima.service.TbActivityService;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TbActivityServiceImpl implements TbActivityService {

    @Resource
    private TbActivityDao tbActivityDao;

    //通过id获取活动渠道列表
    @Override
    public R listSelect(Integer id) {
        LambdaQueryWrapper<TbActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbActivity::getChannel, id);
        List<TbActivity> activityList = tbActivityDao.selectList(wrapper);
        return new R(1, "操作成功", activityList);
    }

    //活动分页展示接口
    @Override
    public R getActivityList(ActivityDto activityDto) {
        IPage<TbActivity> page = new Page<>(activityDto.getPageNum(), activityDto.getPageSize());
        LambdaQueryWrapper<TbActivity> wrapper = new LambdaQueryWrapper<>();
        if (activityDto.getCode() != null) {
            wrapper.eq(TbActivity::getCode, activityDto.getCode());  //活动编号
        }
        if (activityDto.getChannel() != null) {
            wrapper.eq(TbActivity::getChannel, activityDto.getChannel());  //渠道来源
        }
        if (activityDto.getStatus()!=null){
            wrapper.eq(TbActivity::getStatus,activityDto.getStatus());  //活动状态
        }
        if (activityDto.getBeginCreateTime()!=null&&activityDto.getEndCreateTime()!=null){
            wrapper.between(TbActivity::getCreateTime,activityDto.getBeginCreateTime(),activityDto.getEndCreateTime());  //创建日期
        }
        if (activityDto.getBeginTime()!=null&&activityDto.getEndTime()!=null){
            wrapper.ge(TbActivity::getBeginTime,activityDto.getBeginTime());  //大于等于开始日期
            wrapper.le(TbActivity::getEndTime,activityDto.getEndTime());  //小于等于结束日期
        }
        wrapper.orderByDesc(TbActivity::getCreateTime);  //默认数据按添加时间倒序排列
        IPage<TbActivity> activityIPage = tbActivityDao.selectPage(page, wrapper);
        PageResult result = new PageResult();
        result.setPageNum(activityDto.getPageNum());
        result.setPageSize(activityDto.getPageSize());
        result.setPages(activityIPage.getPages());
        result.setTotal(activityIPage.getTotal());
        result.setResults(activityIPage.getRecords());
        return new R(1,MsgResult.SUCCESS_FIND,result);
    }

    //活动信息回显接口
    @Override
    public R findById(Long id) {
        TbActivity tbActivity = tbActivityDao.selectById(id);
        if (tbActivity!=null){
            return new R(1,MsgResult.SUCCESS_FIND,tbActivity);
        }
        return new R(0,MsgResult.WRONG_FIND,"");
    }

    //活动修改接口
    @Override
    public R modify(TbActivity tbActivity) {
        int i = tbActivityDao.updateById(tbActivity);
        if (i>0){
            return new R(1,MsgResult.SUCCESS_MODIFY,"");
        }
        return new R(0,MsgResult.WRONG_FIND,"");
    }

    //活动删除接口
    @Override
    public R remove(Long id) {
        int i = tbActivityDao.deleteById(id);
        if (i>0){
            return new R(1,MsgResult.SUCCESS_REMOVE,"");
        }
        return new R(0,MsgResult.WRONG_REMOVE,"");
    }

    //审核通过接口
    @Override
    public R passActivity(Long id) {
        TbActivity tbActivity = tbActivityDao.selectById(id);
        tbActivity.setStatus("2");
        tbActivityDao.updateById(tbActivity);
        return new R(1,"审核通过","");
    }

    //审核驳回接口
    @Override
    public R reject(Long id) {
        TbActivity tbActivity = tbActivityDao.selectById(id);
        tbActivity.setStatus("3");
        tbActivityDao.updateById(tbActivity);
        return new R(1,"审核已驳回","");
    }

    //活动添加接口
    @Override
    public R add(TbActivity tbActivity) {
        tbActivity.setCreateTime(new Date());
        tbActivity.setStatus("4");
        String code = ValidateCodeUtils.generateValidateCode4String(6);
        tbActivity.setCode(code);
        tbActivityDao.insert(tbActivity);
        return new R(1, MsgResult.SUCCESS_ADD,"");
    }

    //通过活动渠道获取活动列表
    @Override
    public R getByChannel(String channel) {
        LambdaQueryWrapper<TbActivity> lqw=new LambdaQueryWrapper<>();
        lqw.eq(TbActivity::getChannel,channel);
        List<TbActivity> tbActivities = tbActivityDao.selectList(lqw);
        return new R(1,MsgResult.SUCCESS_FIND,tbActivities);
    }

    //获取活动详细信息
    @Override
    public R getById(Long activityId) {
        TbActivity tbActivity = tbActivityDao.selectById(activityId);
        if (tbActivity == null) {
            return new R(0,MsgResult.WRONG_FIND,null);
        }
        return new R(1,MsgResult.SUCCESS_FIND,tbActivity);
    }

    //获取指定时间段内活动对象集合
    @Override
    public List<TbActivity> getTime(Date startTime, Date endTime) {
        List<TbActivity> tbActivities=tbActivityDao.selectByTime(startTime,endTime);
        return tbActivities;
    }
}
